﻿using Ekton_Manage_DomainCore.Aggregate;
using Ekton_Manage_DTO.RequestModels.Material;
using Ekton_Manage_Framework.Methods;
using Ekton_Manage_Framework.Redis;
using Ekton_Manage_IBusiness;
using Ekton_Manage_InfrastructureCore.Common;
using Ekton_Manage_InfrastructureCore.Repositories;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Ekton_Manage_Business
{
    public class SelectMaterialList : ISelectMaterialList
    {

        private readonly SerializeParameter _serialize;
        private readonly IBaseRepository<material, int> _baseMaterialRepository;

        public SelectMaterialList(SerializeParameter serialize, IBaseRepository<material, int> baseMaterialRepository)
        {
            _serialize = serialize;
            _baseMaterialRepository = baseMaterialRepository;
        }

        /// <summary>
        ///  查询全部材料列表
        /// </summary>
        public async Task<Tuple<List<material>, int>> GetMeterialList(QueryDataParame query)
        {
            var dicData = await _serialize.ParameConvertToDict(query.parame);
            Expression<Func<material, bool>> expression = m => true;
            if (dicData.ContainsKey("materials_name") && !string.IsNullOrEmpty(dicData["materials_name"].ToString()))
            {
                expression = expression.ExpressionAnd(m => m.materials_name.Contains(dicData["materials_name"].ToString()));
            }

            if (dicData.ContainsKey("color") && !string.IsNullOrEmpty(dicData["color"].ToString()))
            {
                expression = expression.ExpressionAnd(m => m.color.Contains(dicData["color"].ToString()));
            }

            if (dicData.ContainsKey("remark") && !string.IsNullOrEmpty(dicData["remark"].ToString()))
            {
                expression = expression.ExpressionAnd(m => m.remark.Contains(dicData["remark"].ToString()));
            }

            var result = await _baseMaterialRepository.QueryPageData(query, expression);
            return result;
        }


        /// <summary>
        ///  编辑材料列表
        /// </summary>
        /// 
        public async Task<Message> PutMeterialList(DtoSelectMaterialList request)
        {

            var userInfo = await _baseMaterialRepository.QuerySingleData(request.id);
            if (userInfo == null) return new Message(false, 202);
            userInfo.materials_name = request.materials_name;
            userInfo.color = request.color;
            userInfo.remark = request.remark;
            var result = await _baseMaterialRepository.UpdateSingleData(userInfo);
            return new Message(result.Item1);
        }

        /// <summary>
        ///  添加材料列表
        /// </summary>
        /// 
        public async Task<Message> PostMeterialList(DtoSelectMaterialList request)
        {
            var materials = await _baseMaterialRepository.InsertSingleData(new material
            {
                materials_name = request.materials_name,
                color = request.color,
                remark = request.remark,
            });

            return new Message(materials);
        }

        /// <summary>
        /// 删除材料信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Message> DeleteMeterialInfo(int id)
        {
            var list = await _baseMaterialRepository.DeleteSingleData(id);
            return new Message(list);
        }
    }
}
